package com.java.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
 * 反射java.lang.String类的定义结构
 * @author jack
 *
 */
public class ReflectionTest {

	public static void main(String[] args) {
		Class<?> c1=null;
		try {
			c1=Class.forName("java.lang.String");
			System.out.println("package\t"+c1.getPackage().getName()+";");
			System.out.print(Modifier.toString(c1.getModifiers())+" ");
			System.out.print("class "+c1.getSimpleName()+" ");
			System.out.print("extends "+c1.getSuperclass().getSimpleName()+" ");
			Class<?>[] inter=c1.getInterfaces();
			if(inter.length>0){
				System.out.print("implements ");
				for(int i=0;i<inter.length;i++){
					System.out.print(inter[i].getSimpleName());
					if(i<inter.length-1){
						System.out.print(",");
					}
				}
			}
			System.out.println("{");
			printFields(c1);
			printMethods(c1);
			System.out.println("}");
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	public static void printFields(Class<?> c1){
		Field[] f=c1.getDeclaredFields();
		for(int i=0;i<f.length;i++){
			System.out.print("\t\t"+Modifier.toString(f[i].getModifiers())+" ");
			System.out.print(f[i].getType().getSimpleName()+" ");    
			System.out.print(f[i].getName());
			System.out.println(";");
		}
		
	}
	public static void printMethods(Class<?> c1){
		Method[] m=c1.getDeclaredMethods();
		for(int i=0;i<m.length;i++){
			System.out.print("\t\t"+Modifier.toString(m[i].getModifiers())+" ");
			System.out.print(m[i].getReturnType().getName()+" ");
			System.out.print(m[i].getName()+" ");
			System.out.print("(");
			Class<?>[] c=m[i].getParameterTypes();
			for(int j=0;j<c.length;j++){
				System.out.print(c[j].getName()+" ");
				System.out.print("args"+j);
				if(j<c.length-1){
					System.out.print(",");
				}
			}
			System.out.print(")");
			System.out.print("{");
			System.out.println("\n\t\t\t.........");
			System.out.println("\t\t}\n");
		}
		
	}
	

}
